package com.example.yang.samapp;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Yang on 2016/9/18.
 */
public class dianji extends AppCompatActivity {
    private Button button;
    private static final int UPDATE_BTN_TEXT=0;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what==UPDATE_BTN_TEXT){
                int seconds=(int)msg.obj;
                button.setText("已发送"+seconds+"秒");
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.absketball);
        button = (Button) findViewById(R.id.basketball_btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //new CountDownThead(10).start();
               //countdown();
               new CountDownThead2(30).start();
                //new CountDownThead3(10).start();
            }
        });
    }
    class CountDownThead3 extends Thread{
        private  static final int UPDATE_BTN_TEXT =0;
        private int seconds;
        public CountDownThead3(int secondsInfuture){
            seconds=secondsInfuture;
        }
        @Override
        public void run() {
            while (seconds >0){
                try {
                    Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                }
                    Message msg=new Message();
                  msg.what=  UPDATE_BTN_TEXT;
                    msg.obj=seconds;
                handler.sendMessage(msg);
                (seconds)--;
            }
        }
    }
    //在子线程中使用runOnuiThread来更新按钮文本
    class CountDownThead2 extends Thread{
        private int seconds;
        public CountDownThead2(int secondsInfuture){
            seconds=secondsInfuture;
        }

        @Override
        public void run() {
          for (int i=seconds;i>=0;i--){
              try {
                      Thread.sleep(1000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              final int zuile=i;
              runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      button.setText("已发送"+zuile+"秒");
                  }
              });
              }
           }

        }
//利用递归调用来让Timer延迟执行一个任务
    public void countdown(){
        new Timer("").schedule(new TimerTask() {
            private int num;
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        button.setText("已发送" + num + "s");
                    }
                });
                if (num > 0)
                    countdown();
                num--;
            }
        },1000);
    }



    class CountDownThead extends CountDownTimer{
        private int startSeconds;
        public CountDownThead(int secondsInfuture){

            super(secondsInfuture*1000,1000);
            this.startSeconds=secondsInfuture;
        }
//每隔一个周期.及构造方法里面的第二个参数指定的一秒钟都会自动调用的方法
        @Override
        public void onTick(long millisUntilFinished) {
                button.setText("已发送"+startSeconds+"s");
            startSeconds--;
        }
//计时结束后执行的方法倒
        @Override
        public void onFinish() {
            button.setText("重新发送");
        }
    }
}

